#include <QWidget>
#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    QWidget *widget = new QWidget;
    widget->setStyleSheet("QWidget{background-color:yellow;}");
    ui->gridLayout->addWidget(widget, 0, 0);

    QWidget *widget2 = new QWidget;
    widget2->setStyleSheet("QWidget{background-color:green;}");
    ui->gridLayout->addWidget(widget2, 0, 1);

    QWidget *widget3 = new QWidget;
    widget3->setStyleSheet("QWidget{background-color:pink;}");
    ui->gridLayout->addWidget(widget3, 1, 0);

    QWidget *widget4 = new QWidget;
    widget4->setStyleSheet("QWidget{background-color:purple;}");
    ui->gridLayout->addWidget(widget4, 1, 1);

}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::on_pushButton_clicked()
{
    static int row = 0, column = 0;
    QWidget *widget = new QWidget;
    widget->setStyleSheet("QWidget{background-color:red;}");
    ui->gridLayout->addWidget(widget, row, column++);
    if (column > row)
        row++;
}
